package main

import (
	"net/http"

	"gdzce.cn/supplychain-finance/application/blockchain"
	"gdzce.cn/supplychain-finance/application/controller"
	"github.com/gin-gonic/gin"
)

//const (
//	TransactionRecordFileName = "transactionRecord.json" // 存储 [{orderId，txid}] 数组的json文件名
//)

// 设置路由
func setupRouter() *gin.Engine {
	//gin.SetMode(gin.ReleaseMode)			// 开启生产模式（关闭DEBUG模式）
	router := gin.Default()

	// 加载中间件
	router.Use(controller.CORS)         // 先加载跨域模块（便于开发）
	router.Use(controller.FixVueRouter) // 修复前端内部路由问题

	// 定义路由（当地址匹配时，调用相应函数）
	router.GET("/getTransactions", controller.GetTransactions)
	router.GET("/get/:companyType", controller.GetCompany)
	router.GET("/getTransactionHistory", controller.GetTransactionHistory)
	router.GET("/getTrByBuyer", controller.GetTrByBuyer)

	router.POST("/newTransaction", controller.NewTransaction)
	router.POST("/confirmTransaction", controller.ConfirmTransaction)
	router.POST("/applyPayForAnother", controller.ApplyPayForAnother)
	router.POST("/payTransaction", controller.PayTransaction)
	router.POST("/affirmPay", controller.AffirmPay)
	router.POST("/refusePayTransaction", controller.RefusePayTransaction)
	router.POST("/updateShipmentInfo", controller.UpdateShipmentInfo)
	router.POST("/verifyShipmentInfo", controller.VerifyShipmentInfo)
	router.POST("/repayTransaction", controller.RepayTransaction)
	router.POST("/verifyRepay", controller.VerifyRepay)

	router.POST("/createCompany", controller.CreateCompany)
	router.POST("/login", controller.Login)

	router.GET("/queryBlockByTxID", controller.QueryBlockByTxID)
	router.GET("/queryTransaction", controller.QueryTransaction)

	// 静态文件路由
	router.StaticFS("/web/", http.Dir("./public_v2/"))

	// 返回路由对象
	return router
}

func main() {
	// 初始化fabric sdk
	blockchain.Init()

	// 加载存储[{orderId，txid}]的json文件
	//repository.TransactionRecordList.FilePath = TransactionRecordFileName
	//_ = repository.TransactionRecordList.LoadTransactionRecords()

	// 加载路由
	router := setupRouter()

	// 启动http服务器
	err := router.Run(":8080") // listen and serve on 0.0.0.0:8080
	if err != nil {
		panic(err)
	}
}
